package com.chianghao.rockframewrok.starter.db.properties;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

@ConfigurationProperties(prefix = "chianghao.mybatis", ignoreUnknownFields = true)
public class MybatisProperties {

	private String configLocation;
	
	private String[] mapperLocations = new String[]{"classpath*:/com/**/mapper/**/*.xml"};

	public String getConfigLocation() {
		return configLocation;
	}

	public void setConfigLocation(String configLocation) {
		this.configLocation = configLocation;
	}

	public String[] getMapperLocations() {
		return mapperLocations;
	}

	public void setMapperLocations(String[] mapperLocations) {
		this.mapperLocations = mapperLocations;
	}
	
	public Resource[] resolveMapperLocations() {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		List<Resource> resourcesList = new ArrayList<Resource>();
		for(String location:this.mapperLocations) {
			try {
				for(Resource r: resolver.getResources(location)) {
					resourcesList.add(r);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Resource[] resourceArray = new Resource[resourcesList.size()];
		return resourcesList.toArray(resourceArray);
    }
	
}
